package com.blackfish.api;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

import com.blackfish.common.plugin.ClientHelper;
import com.jfinal.plugin.redis.Redis;
import org.json.JSONObject;

import com.jfinal.core.*;
import com.jfinal.json.FastJson;
import com.jfinal.kit.HttpKit;
import sun.misc.Cache;
import sun.text.resources.FormatData;

public class ApiController extends Controller {
    private ApiService service = new ApiService();
    private static ClientHelper clientHelper = new ClientHelper();
    public int portMaxNum= 10000;

    public void test() {
        String i = getPara("test");
        if (i.equals("1")) {
            renderText("OK");
        } else {
            renderText("Error");
        }
    }

    // 客户端得到UDP的广播后将IM服务器的IP地址和port返回给服务器,如果IP重复则不再新建链接
    public void imConnect() throws Exception {
        String imIp = getPara("imIp");// IM系统中的ID不是数据库中的设备ID
        String imPort = getPara("imPort");

        com.jfinal.plugin.redis.Cache eventCache = Redis.use("event");

        long fp = eventCache.sadd("ip:imIp",imIp);

        // 同时链接端口数
        if(eventCache.scard("ip:imIp")>portMaxNum){//||fp==0){
            renderText("WARNING");
        }else if(!clientHelper.addImConnection(imIp,Integer.valueOf(imPort))){
            renderText("ERROR");
        }else{
            renderText("OK");
        }
    }

    public void Connect() {
        // 保存设备ip地址
        String imId = getPara("imId");// IM系统中的ID不是数据库中的设备ID
        // String deviceId = getPara("id");
        String deviceKey = getPara("initKey");
        // 保存im系统的ip
        String imIp = getPara("imIp");
        int imPort = getParaToInt("imPort");
        com.jfinal.plugin.redis.Cache eventCache = Redis.use("event");
        eventCache.lpush("id:"+deviceKey,imIp,imId,imPort);
        File xml = new File(getSession().getServletContext().getRealPath("files/init.xml"));
        xml.setWritable(true);
        service.initXml(xml,deviceKey);
        renderFile(xml);
    }

//    public void initDevice(){
//
//    }


    //map的value装上json，key为了汇报
//    public void listen() {
//        String passKey = getPara("key");
//        String json = service.getEventList(passKey);
//        if (json != null) {
//            renderText(json);
//        } else {
//            renderText("Null");
//        }
//    }

//    public void reply() {
//        //String passKey = getPara("time");
//        String passKey = getPara("id");
//        if (service.delEventlist(passKey)) {
//            renderText("Clear");
//        } else {
//            renderText("Busy");
//        }
//    }

    public void request() {
        String json = HttpKit.readData(getRequest());
        //json = json.substring(1, json.length()-1);
        System.out.println(json.toString());
        JSONObject jsonObj = new JSONObject(json);
        String path = jsonObj.optString("path");
        String write = jsonObj.optString("write");
        File file = new File(path);
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            FileWriter fileWriter = new FileWriter(file);
            fileWriter.write(write);
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        renderText("Null");
    }
}
